﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using YBApproval.IDAL;
using YBApproval.Model;

namespace YBApproval.EFDAL
{
    public class InstanceDAL : IInstance
    {
        private YBDbContext _db = new YBDbContext();
        private InstanceValueDAL _ivDAL = new InstanceValueDAL();
        private InstanceApprovalDAL _iaDAL = new InstanceApprovalDAL();

        public Instance GetInstance(string recordId)
        {
            return _db.Instances.Find(recordId);
        }

        public void Add(Instance instance, List<InstanceValue> values, List<InstanceApproval> approvals)
        {
            // insert instance record
            _db.Instances.Add(instance);

            // insert instance value records
            for (int i = 0; i < values.Count; i++)
            {
                _db.InstanceValues.Add(values[i]);
            }

            // insert instance approval records
            for (int i = 0; i < approvals.Count; i++)
            {
                _db.InstanceApprovals.Add(approvals[i]);
            }

            // save to DB in one time (EF will use transaction)
            _db.SaveChanges();
        }


        /// <summary>
        /// 添加草稿
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="values"></param>
        public void Add(Instance instance, List<InstanceValue> values)
        {
            // insert instance record
            _db.Instances.Add(instance);

            // insert instance value records
            for (int i = 0; i < values.Count; i++)
            {
                _db.InstanceValues.Add(values[i]);
            }

            // save to DB in one time (EF will use transaction)
            _db.SaveChanges();
        }





        public void Update(Instance instance, List<InstanceValue> values, List<InstanceApproval> approvals, List<InstanceApproval> oldApprovals)
        {
            _db.Entry(instance).State = EntityState.Modified;

            // delete all existing instance value records
            List<InstanceValue> oldValues = _db.InstanceValues.Where(iv => iv.RecordId == instance.RecordId).ToList();
            for (int i = 0; i < oldValues.Count; i++)
            {
                _db.InstanceValues.Remove(oldValues[i]);
            }
            // insert instance value records
            for (int i = 0; i < values.Count; i++)
            {
                _db.InstanceValues.Add(values[i]);
            }

            // update all existing instance approval records
            if (oldApprovals != null)
            {
                for (int i = 0; i < oldApprovals.Count; i++)
                {
                    _db.Entry(oldApprovals[i]).State = EntityState.Modified;
                }
            }
            // insert instance approval records
            for (int i = 0; i < approvals.Count; i++)
            {
                _db.InstanceApprovals.Add(approvals[i]);
            }

            _db.SaveChanges();
        }

        public void Update(Instance instance)
        {
            _db.Entry(instance).State = EntityState.Modified;
            _db.SaveChanges();
        }

        public void Update(Instance instance, InstanceApproval instApproval, List<InstanceApproval> approvals)
        {
            // 更新instance
            _db.Entry(instance).State = EntityState.Modified;

            // 更新 instance approval，用户编辑了comment，设置了是否同意等信息
            _db.Entry(instApproval).State = EntityState.Modified;

            // 新增 instance approval 数据，为下次审批准备
            for (int i = 0; i < approvals.Count; i++)
            {
                _db.InstanceApprovals.Add(approvals[i]);
            }

            _db.SaveChanges();
        }

        public void Update(Instance instance, InstanceApproval instApproval)
        {
            // 更新instance，被Close掉了
            _db.Entry(instance).State = EntityState.Modified;

            // 更新 instance approval，用户编辑了comment，设置了是否同意等信息
            _db.Entry(instApproval).State = EntityState.Modified;

            _db.SaveChanges();
        }



        /// <summary>
        /// 更新草稿
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="values"></param>
        public void Update(Instance instance, List<InstanceValue> values)
        {
            _db.Entry(instance).State = EntityState.Modified;

            // delete all existing instance value records
            List<InstanceValue> oldValues = _db.InstanceValues.Where(iv => iv.RecordId == instance.RecordId).ToList();
            for (int i = 0; i < oldValues.Count; i++)
            {
                _db.InstanceValues.Remove(oldValues[i]);
            }
            // insert instance value records
            for (int i = 0; i < values.Count; i++)
            {
                _db.InstanceValues.Add(values[i]);
            }

            _db.SaveChanges();
        }


        public void Delete(string recordId)
        {
            //删除Instance表中的数据
            Instance instance = _db.Instances.FirstOrDefault(i => i.RecordId == recordId && i.IsSubmitted == false);
            if (instance == null)
            {
                return;
            }            
            _db.Entry<Instance>(instance).State = EntityState.Deleted;
            //删除InstanceApproval表中数据
            var approvalIds = from ia in _db.InstanceApprovals
                              where ia.RecordId == recordId
                              select ia.Id;
            foreach (int approvalId in approvalIds)
            {
                InstanceApproval ia = new InstanceApproval { Id = approvalId };
                _db.Entry<InstanceApproval>(ia).State = EntityState.Deleted;
            }
            //删除InstanceValue表中数据
            var valueIds = from iv in _db.InstanceValues
                         where iv.RecordId == recordId
                         select iv.Id;
            foreach (int valueId in valueIds)
            {
                InstanceValue iv = new InstanceValue { Id = valueId };
                _db.Entry<InstanceValue>(iv).State = EntityState.Deleted;
            }
            _db.SaveChanges();
        }



        public void Dispose()
        {
            _db.Dispose();
        }


        
    }
}
